VERSION 5.00
Begin VB.Form frmODBCProcess 
   Caption         =   "ODBCProcess"
   ClientHeight    =   3135
   ClientLeft      =   60
   ClientTop       =   405
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3135
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox textMsg 
      Height          =   2295
      Left            =   240
      Locked          =   -1  'True
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   0
      TabStop         =   0   'False
      Text            =   "frmODBCProcess.frx":0000
      Top             =   240
      Width           =   3375
   End
   Begin VB.Timer timerProcess 
      Enabled         =   0   'False
      Interval        =   200
      Left            =   4080
      Top             =   1080
   End
End
Attribute VB_Name = "frmODBCProcess"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Option Explicit

#If Livingston Then


Public m_boolInProcess As Boolean
Dim mstrIMPORT_FILE_PATH As String
Dim mobjFSO As FileSystemObject
Public mstrState As String
Dim mdtStatusTime As Date
' Don't get stuck on first file in folder
Dim mlngFileCount As Long
Dim mstrStateSave As String
'

Private Sub Form_Initialize()

    On Error GoTo ErrorExit
    
    If GetSystemName = "CAN00283812" Then
        g_clsSettings.Setting("IntegrationFolder") = "C:\Documents and Settings\271425\My Documents\CUSTOMERDATA\CUSTOMERDATA\Livingston\"
        MsgBox "integrationFolder set to " & g_clsSettings.Setting("IntegrationFolder")
    ElseIf GetSystemName = "WINXPC9CA96EA3" Then
        g_clsSettings.Setting("IntegrationFolder") = "C:\Documents and Settings\563774\My Documents\FXI\Projects\Meena\Livingston\"
        MsgBox "IntegrationFolder set to " & g_clsSettings.Setting("IntegrationFolder")
    End If

    mstrIMPORT_FILE_PATH = g_clsSettings.Setting("IntegrationFolder")
    If mstrIMPORT_FILE_PATH = "" Then
        If GetSystemName = "CAN00283812" Then
            g_clsSettings.Setting("IntegrationFolder") = "C:\Documents and Settings\271425\My Documents\CUSTOMERDATA\CUSTOMERDATA\Livingston\"
        ElseIf GetSystemName = "WINXPC9CA96EA3" Then
            g_clsSettings.Setting("IntegrationFolder") = "C:\Documents and Settings\563774\My Documents\FXI\Projects\Meena\Livingston\"
        Else
            g_clsSettings.Setting("IntegrationFolder") = "\\tpcathena\Public\Navision\FedEx\In\"
        End If
        g_clsSettings.SaveSettings
        mstrIMPORT_FILE_PATH = g_clsSettings.Setting("IntegrationFolder")
        DebugPrint "Integration folder " & mstrIMPORT_FILE_PATH
        frmMsgBox.ShowMessage "Configuration file (FSMSClient.xml) updated with IntegrationFolder.", 8000
    End If
    
    mdtStatusTime = Now
    mstrState = "Start"
    
    DebugPrint "ODBCProcess Init."
    
    Exit Sub
ErrorExit:
    MsgBox Err.Description, , "ODBCProcess.Initialize"

End Sub

Private Sub Form_Load()

    DebugPrint "ODBCProcess Load."
    
    Me.timerProcess.Enabled = True

End Sub

Private Sub timerProcess_Timer()

    On Error GoTo ErrorExit
    
    Dim lngCount As Long
    Dim objfile As File

    g_strErrorLocation = "Timer"

    If mstrState <> mstrStateSave Then
        DebugPrint "State change from " & mstrStateSave & " to " & mstrState
        mstrStateSave = mstrState
    End If
    
    ' 2010-08-31, JFW, At Livingston server (only) I'm getting an additional thread on this timer.
    If glngThreadID <> 0 Then
        DebugPrint "ODBCProcess, Timer, invalid thread."
        Exit Sub
    End If
    glngThreadID = App.ThreadID
    
    timerProcess.Enabled = False
    If mobjFSO Is Nothing Then
        Set mobjFSO = New FileSystemObject
    End If
    
    Select Case mstrState
    Case "Start"
        ' Client is warming up or getting connected.
        mdtStatusTime = Now
        
    Case "Connect"
        ' Socket connect to server, give 2 seconds before action
        If DateAdd("s", 5, mdtStatusTime) < Now Then
            mstrState = "Idle"
            mdtStatusTime = Now
        End If
        
    Case "Idle"
        For Each objfile In mobjFSO.GetFolder(mstrIMPORT_FILE_PATH).Files
            If mstrState <> "Idle" Then Exit For
            If LCase(Right(objfile.name, 4)) = ".txt" Then
                If lngCount = mlngFileCount Then
                    mlngFileCount = mlngFileCount + 1
                    DebugPrint "Processing file " & objfile.Path
                
                    g_strKeyFile = objfile.Path
                    mstrState = "FileMove"
                Else
                    lngCount = lngCount + 1
                End If
            End If
        Next objfile
        ' No files found to process
        mlngFileCount = 0
        
    Case "FileMove"
        g_strErrorLocation = "FileMove"
        If mobjFSO.FileExists(g_strKeyFile) Then
            Set objfile = mobjFSO.GetFile(g_strKeyFile)
            If Not mobjFSO.FolderExists(mstrIMPORT_FILE_PATH & "\Pending") Then
                mobjFSO.CreateFolder mstrIMPORT_FILE_PATH & "\Pending"
            End If
            If Not mobjFSO.FolderExists(mstrIMPORT_FILE_PATH & "\Done") Then
                mobjFSO.CreateFolder mstrIMPORT_FILE_PATH & "\Done"
            End If
            If mobjFSO.FileExists(mstrIMPORT_FILE_PATH & "\Pending\" & objfile.name) Then
                DebugPrint "Delete file in pending folder, " & mstrIMPORT_FILE_PATH & "\Pending\" & objfile.name
                mobjFSO.DeleteFile mstrIMPORT_FILE_PATH & "\Pending\" & objfile.name, True
            End If
            DebugPrint "Moving to pending folder, " & mstrIMPORT_FILE_PATH & "\Pending\" & objfile.name
            objfile.Move mstrIMPORT_FILE_PATH & "\Pending\" & objfile.name
            g_strKeyFile = mstrIMPORT_FILE_PATH & "\Pending\" & objfile.name
            If mobjFSO.FileExists(mstrIMPORT_FILE_PATH & "\Done\" & objfile.name) Then
                ' File found, trying Reprint
                DebugPrint "ODBCProcess, Timer, File found, going to reprint" & ", " & g_strKeyFile
                mstrState = "GetDataReprint"
            Else
                mstrState = "GetData"
            End If
        Else
            ' File not found
            DebugPrint "ODBCProcess, Timer, File not found, state " & mstrState & ", " & g_strKeyFile
            mstrState = "Idle"
        End If
            
    Case "GetData"
        g_strErrorLocation = "GetData"
        If mobjFSO.FileExists(g_strKeyFile) Then
            Set objfile = mobjFSO.GetFile(g_strKeyFile)
            DebugPrint "GetData, " & g_strKeyFile
            If GetData(g_strKeyFile) Then
                mstrState = "Screen"
                mdtStatusTime = Now
            Else
                mstrState = "ShipError"
                mdtStatusTime = Now
            End If
        Else
            ' File not found
            DebugPrint "ODBCProcess, Timer, File not found, state " & mstrState & ", " & g_strKeyFile
            mstrState = "Idle"
        End If
        
    Case "GetDataReprint"
        g_strErrorLocation = "GetDataReprint"
        If mobjFSO.FileExists(g_strKeyFile) Then
            Set objfile = mobjFSO.GetFile(g_strKeyFile)
            DebugPrint "File reprint, " & g_strKeyFile
            mstrState = GetDataReprint(g_strKeyFile)
            mdtStatusTime = Now
        Else
            ' File not found
            DebugPrint "ODBCProcess, Timer, File not found, state " & mstrState & ", " & g_strKeyFile
            mstrState = "Idle"
        End If
        
    Case "Screen"
        ' For testing add x seconds before pressing the Ship button
        'If DateAdd("s", 1, mdtStatusTime) < Now Then
            DebugPrint "ODBCProcess, Timer, Ship button, " & g_strKeyFile
            mstrState = "Wait"
            mdtStatusTime = Now
            Call frmShip.ShipButton
        'End If
        
    Case "Wait"
        ' Ship button pressed, give the server 10 seconds to respond.
        If DateAdd("s", 20, mdtStatusTime) < Now Then
            MsgBox "Timeout on server ship reply."
            mstrState = "ShipError"
            mdtStatusTime = Now
        End If
        
    Case "ShipError"
        g_strErrorLocation = "ShipError"
        DebugPrint "ODBCProcess, Timer, ShipError, delete file " & g_strKeyFile
        If mobjFSO.FileExists(g_strKeyFile) Then
            mobjFSO.DeleteFile g_strKeyFile, True
            g_strKeyFile = ""
        Else
            DebugPrint "ODBCProcess, Timer, ShipError, file not found, file " & g_strKeyFile
        End If
        frmShip.ClearButton
        mstrState = "Idle"
        mdtStatusTime = Now
            
    Case "Shipped"
        g_strErrorLocation = "Shipped"
        If mobjFSO.FileExists(g_strKeyFile) Then
            DebugPrint "ODBCProcess, Timer, Shipped, move to done, file " & g_strKeyFile
            Set objfile = mobjFSO.GetFile(g_strKeyFile)
            If Not mobjFSO.FolderExists(mstrIMPORT_FILE_PATH & "\Done") Then
                mobjFSO.CreateFolder mstrIMPORT_FILE_PATH & "\Done"
            End If
            DebugPrint "Moving to done folder, " & mstrIMPORT_FILE_PATH & "\Done\" & objfile.name
            objfile.Move mstrIMPORT_FILE_PATH & "\Done\" & objfile.name
            g_strKeyFile = mstrIMPORT_FILE_PATH & "\Done\" & objfile.name
        Else
            DebugPrint "ODBCProcess, Timer, Shipped, file not found, file " & g_strKeyFile
        End If
        mstrState = "Idle"
        mlngFileCount = 0
        
        
    Case "Reprinted"
        g_strErrorLocation = "Reprinted"
        If mobjFSO.FileExists(g_strKeyFile) Then
            DebugPrint "ODBCProcess, Timer, Reprinted, delete, file " & g_strKeyFile
            mobjFSO.DeleteFile g_strKeyFile, True
        Else
            DebugPrint "ODBCProcess, Timer, Reprinted, file not found, file " & g_strKeyFile
        End If
        mstrState = "Idle"
        mlngFileCount = 0
        
    Case Else
        mstrState = "Idle"
        
    End Select
    
ExitHere:
    glngThreadID = 0
    timerProcess.Enabled = True
    Exit Sub
    
ErrorExit:
    MsgBox mstrIMPORT_FILE_PATH
    MsgBox "Error in Process Timer, status " & mstrState & ", step " & g_strErrorLocation & ", " & Err.Description, , "ODBCProcess.Timer"
    mstrState = "Idle"
    GoTo ExitHere
    
End Sub

#End If

